package com.dalin.framework.dao;

import com.dalin.framework.dao.imple.MysqlConnection;
import com.dalin.framework.dao.imple.OracleConnection;
import com.dalin.framework.dao.imple.SqlserverConnection;
import com.dalin.framework.vos.JdbcVo;
import com.dalin.framework.common.PropertyConst;

import java.sql.Connection;
import java.sql.SQLException;

/*****************************************************************************
 * @content: 数据库连接工厂                                                   *
 * @author : 那建林                                                          *
 * @crtDate: 2009-06-15                                                     *
 ****************************************************************************/
public class ConnectFactory {
	private ConnectFactory() { // 不允许new此对象
	}
	
	/*************************************************************************
	 * @content: 根据JdbvVo值对象来动态创建相应的DB连接
	 * @param  : JdbcVo
	 * @return : DbConn接口
	 * @author : 那建林
	 * @crtDate: 2009-06-15
	 */
	public static DbConn getConnectionInstance(JdbcVo vo) {
		String driverClassName = getDriverClassName(vo.getDbType()); // vo driver串
		String name = vo.getUserName(); // 登陆用户名
		String pswd = vo.getUserPswd(); // 登陆密码
		String url = null; // 连接路径
		if (PropertyConst.DRIVER_MSSQL_2000.equals(driverClassName)) {
			url = "jdbc:microsoft:sqlserver://" + vo.getDbIp()
				  + PropertyConst.COLON + vo.getDbPort()
				  + ";SelectMethod=cursor;DatabaseName=" + vo.getDbName();
			return new SqlserverConnection(driverClassName, url, name, pswd);
		} else if (PropertyConst.DRIVER_MSSQL_2005.equals(driverClassName)){
			url = "jdbc:sqlserver://" + vo.getDbIp()
				  + PropertyConst.COLON + vo.getDbPort()
				  + ";databaseName=" + vo.getDbName();
			return new SqlserverConnection(driverClassName, url, name, pswd);
		} else if (PropertyConst.DRIVER_ORACLE.equals(driverClassName)){
			url = "jdbc:oracle:thin:@" + vo.getDbIp()  
				  + PropertyConst.COLON + vo.getDbPort()
				  + PropertyConst.COLON + vo.getDbName();
			return new OracleConnection(driverClassName, url, name, pswd);
		} else if (PropertyConst.DRIVER_MYSQL.equals(driverClassName)){
			url = String.format("jdbc:%s://%s:%s/%s", vo.getDbType(),
					vo.getDbIp(), vo.getDbPort(), vo.getDbName())+"?useUnicode=true&characterEncoding=utf8";
			System.out.println("Connecting to database==" + url);
			return new MysqlConnection(driverClassName, url, name, pswd);
		}
		return null;
	}

	/**
	 * 根据数据库类型获取对应的驱动类名。
	 *
	 * @param dbType 数据库类型（如 "mysql"、"postgresql" 等）。
	 * @return 对应的驱动类名。
	 */
	private static String getDriverClassName(String dbType) {
        return switch (dbType.toLowerCase()) {
            case "mysql" -> PropertyConst.DRIVER_MYSQL;
            case "postgresql" -> "org.postgresql.Driver";
            case "oracle" -> "oracle.jdbc.driver.OracleDriver";
            case "sqlserver" -> "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            default ->
                    throw new IllegalArgumentException("不支持的数据库类型: " + dbType);
        };
	}


	public static void main(String[] args) throws Exception {
		JdbcVo jv = new JdbcVo();
		jv.setDbType("mysql");
		jv.setDbIp("172.16.29.196");
		jv.setDbPort("3306");
		jv.setUserName("root");
		jv.setUserPswd("password");
		jv.setDbName("mybatis");
		DbConn conn = getConnectionInstance(jv);
		System.out.println(conn);
		System.out.println(conn.connectTest());
		//conn.close();
	}
}
